﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace hoangnam12
{
    public partial class _Default : System.Web.UI.Page
    {
      
        protected void Page_Load(object sender, EventArgs e)
        {
            DataSet ds = SampleData();
            clPager.PageSize = 2; // số sản phẩm mỗi trang
            clPager.DataSource = ds.Tables[0].DefaultView; // lấy dữ liệu từ database
            clPager.BindToControl = DataList2; // có thể dùng cho repeater hoặc datalist..
            DataList2.DataSource = clPager.DataSourcePaged;
            
            
        }
       
        #region Create Sample DataSet

        private DataSet SampleData()
        {
            string sql = "SELECT top 30 [MaSP], [TenSP], [GiaSP], [HinhSP] FROM [SanPham] order by [NgayNhap] desc ";
            string constring = "Data Source=ADMIN;Initial Catalog=QLBH;Integrated Security=True";
            SqlConnection conn = new SqlConnection(constring);
            
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);


                DataSet ds = new DataSet();

                da.Fill(ds);
                DataTable dt = ds.Tables.Add("SampleData");

                dt.Columns.Add("MaSP", typeof(string));
                dt.Columns.Add("TenSP", typeof(string));
                dt.Columns.Add("GiaSP", typeof(string));
                dt.Columns.Add("HinhSP", typeof(string));

                return ds;
        

                conn.Close();
            
            
            
        }
        #endregion

        #region list2
        private DataSet SampleData2()
        {
           

            string sql = "SELECT  [MaSP], [TenSP], [GiaSP], [HinhSP] FROM [SanPham] where MaSP in (select top 2 MaSP from ChiTietHoaDon group by MASP order by COUNT(MaSP))";
            string constring = "Data Source=ADMIN;Initial Catalog=QLBH;Integrated Security=True";
            SqlConnection conn = new SqlConnection(constring);
                
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);


                DataSet ds = new DataSet();

                da.Fill(ds);
                DataTable dt = ds.Tables.Add("SampleData2");

                dt.Columns.Add("MaSP", typeof(string));
                dt.Columns.Add("TenSP", typeof(string));
                dt.Columns.Add("GiaSP", typeof(string));
                dt.Columns.Add("HinhSP", typeof(string));

                return ds;
        

                conn.Close();
            
            
            
        }
        #endregion

        protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            
            if (e.CommandName == "Chonmua")
                if (Session["login"] == null)
                    Response.Redirect("DangNhap.aspx");
                else
                {
                    string idSP = DataList1.DataKeys[e.Item.ItemIndex].ToString();
                    double Dongia = double.Parse(((Label)e.Item.FindControl("GiaSPLabel")).Text);
                    string TenSP = ((HyperLink)e.Item.FindControl("HyperLink1")).Text;
                    int Soluong = 1;
                    DataTable dt;
                    if (Session["GioHang"] == null)
                    {
                        dt = new DataTable();
                        dt.Columns.Add("idSP");
                        dt.Columns.Add("TenSP");
                        dt.Columns.Add("Gia");
                        dt.Columns.Add("SoLuong");
                        dt.Columns.Add("TongTien");
                    }
                    else
                        dt = (DataTable)Session["GioHang"];
                    int dong = -1;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["idSP"].ToString() == idSP)
                        {
                            dong = i;
                            break;
                        }
                    }
                    if (dong != -1)
                    {
                        dt.Rows[dong]["SoLuong"] = Convert.ToInt32(dt.Rows[dong]["SoLuong"]) + Soluong;
                    }
                    else
                    {
                        DataRow dr = dt.NewRow();
                        dr["idSP"] = idSP;
                        dr["TenSP"] = TenSP;
                        dr["Gia"] = Dongia;
                        dr["SoLuong"] = Soluong;
                        dr["TongTien"] = Dongia * Soluong;
                        dt.Rows.Add(dr);
                    }
                    Session["GioHang"] = dt;
                    Response.Write("<script>alert('Đã thêm vào giỏ hàng ^.*!...')</script>");

                }
        }

        protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Chonmua")
                if (Session["login"] == null)
                    Response.Redirect("DangNhap.aspx");
                else
                {
                    string idSP = DataList2.DataKeys[e.Item.ItemIndex].ToString();
                    double Dongia = double.Parse(((Label)e.Item.FindControl("GiaSPLabel")).Text);
                    string TenSP = ((HyperLink)e.Item.FindControl("HyperLink1")).Text;
                    int Soluong = 1;
                    DataTable dt;
                    if (Session["GioHang"] == null)
                    {
                        dt = new DataTable();
                        dt.Columns.Add("idSP");
                        dt.Columns.Add("TenSP");
                        dt.Columns.Add("Gia");
                        dt.Columns.Add("SoLuong");
                        dt.Columns.Add("TongTien");
                    }
                    else
                        dt = (DataTable)Session["GioHang"];
                    int dong = -1;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["idSP"].ToString() == idSP)
                        {
                            dong = i;
                            break;
                        }
                    }
                    if (dong != -1)
                    {
                        dt.Rows[dong]["SoLuong"] = Convert.ToInt32(dt.Rows[dong]["SoLuong"]) + Soluong;
                    }
                    else
                    {
                        DataRow dr = dt.NewRow();
                        dr["idSP"] = idSP;
                        dr["TenSP"] = TenSP;
                        dr["Gia"] = Dongia;
                        dr["SoLuong"] = Soluong;
                        dr["TongTien"] = Dongia * Soluong;
                        dt.Rows.Add(dr);
                    }
                    Session["GioHang"] = dt;
                    Response.Write("<script>alert('Đã thêm vào giỏ hàng ^.*!...')</script>");

                }
        }
          
    
    }
    
}
                

        

      
      
       
      


      
    

